﻿/// <reference path="../../Shared/jquery-1.4.1-vsdoc.js" />
/// <reference path="../../Shared/wwtfly.main.js" />
var $msg = {};
var xiaoImgAjaxLoading;
$(function () {

    //隔行换色
    $wwtfly.renderTable($("div.grid_list").find("table"), "alter");
    initMsg(); //初始化提示信息
    initAjax();
    createMenu(); //创建菜单

    initCommand(); //重置
    $(":button[name='btn_reset_search']").click(function () {
        document.location.href = $msg["SearchReset"];
    });
});

//初始化提示信息
var initMsg = function () {

    $msg["AjaxFailure"] = "请求发生错误, 请尝试刷新页面或联系管理员.";
    $msg["AjaxProcess"] = "正在处理请求, 请稍后....";
    $msg["Unload"] = "当前页面的数据有改动, 确定不保存就退出吗?";
    $msg["DeleteFailure"] = "删除失败!";

}

//初始化 有命令的事件..
var initCommand = function () {
    $(".grid_list").find("a[command]").click(function (e) {
        var t = $(this);
        var command = t.attr("command");
        switch (command) {
            case "delete":
                if (confirm("你确定要删除吗?")) {
                    $.get(t.attr("href"), { command: command }, function (resultJson) {
                        //var resultJson = eval("("+result+")");
                        if (resultJson != undefined) {
                            if (resultJson.ResultCode == 100 &&
                        (resultJson.ResultMessage == null || resultJson.ResultMessage == "")) {
                                document.location.reload();
                            } else {
                                if (resultJson.ResultMessage != null && resultJson.ResultMessage != "") {
                                    alert(resultJson.ResultMessage);
                                }

                            }
                        } else {
                            alert($msg["DeleteFailure"]);
                        }
                    });
                }
                break;
            default:
        }
        e.preventDefault();
    });

}


//初始化 ajax相关
var initAjax = function () {
    //全局 jQuery 中 ajax 请求参数
    $.ajaxSetup({ cache: false });
    xiaoImgAjaxLoading = $("<img>", { src: $msg["AppRootPath"] + "Content/Shared/Image/Ajax/xiao001.gif" });
    $(document).ajaxStart(wwtflyBackAjaxStart).ajaxError(wwtflyBackAjaxError).ajaxSuccess(function () {
        $("#ajax_loading").remove();
    });
}

var createMenu = function () { // 菜单操作
    var menu = new MenuClass({ arrowUrl: $msg["AppRootPath"] + "Content/Shared/Image/Menu/right.png" });
    menu.Apply();
    try { window.CreateMenuItems(menu); } catch (e) { }
}
//重置
var wwtflyResetSearch = function () {
    document.location.href = $msg["SearchReset"];
}

var wwtflyBackAjaxStart = function () {
    var img = $("<img>", { src: $msg["AppRootPath"] + "Content/Shared/Image/Ajax/Loading.gif" });
    var load = $("#ajax_loading");
    if (load.length >= 1) {
        load.remove();
    }
    load = $("<div>", { id: "ajax_loading" }).css({ display: "none" }).appendTo($(document.body));
    load.html("");
    load.append(img).css({
        position: "absolute",
        zIndex: "9999",
        textAlign: "center",
        backgroundColor: "#fff",
        border: "solid 4px #ebebeb",
        padding: "15px",
        fontSize: "14px"
    }).append($msg["AjaxProcess"]).css({
        top: $(window).height() / 2 + $(window).scrollTop(),
        left: ($(window).width() - load.outerWidth()) / 2 + $(window).scrollLeft()
    });
    load.css({ display: "block" });
};

var wwtflyBackAjaxError =function (event, XMLHttpRequest, ajaxOptions, thrownError) {
        //            var ajax_load = $("#ajax_loading");
        //            ajax_load.html($msg["AjaxFailure"]);
        //            window.setTimeout(function () {
        //                ajax_load.remove();
        //            }, 3000);

        var ajax_load = $("#ajax_loading").attr("title",
                        "出错了哎,双击关闭哦" +
                        ";\n url:" + ajaxOptions.url +
                        ";\n type:" + ajaxOptions.type +
                        ";\n data:" + ajaxOptions.data +
                        ";\n status:" + XMLHttpRequest.status
                        );
        var ajaxFailure = $msg["AjaxFailure"];
        var info =
                        ajax_load.html("").html((ajaxFailure == undefined ? "" : ajaxFailure)).append(xiaoImgAjaxLoading).append(XMLHttpRequest.responseText)
                        .dblclick(function () {
                            ajax_load.remove();
                        });
        var winH = $(window).height();
        var winW = $(window).width();
        var outerH = ajax_load.outerHeight(true);
        var outerW = ajax_load.outerWidth(true);
        var top = (winH - outerH) / 2;
        top = top < 0 ? 88 : top;
        top += $(window).scrollTop();

        var left = (winW - outerW) / 2;
        left = left <= 0 ? 88 : top;
        left += $(window).scrollLeft();

        ajax_load.css({
            top: top,
            left: left,
            width: outerW > winW ? (winW - 88 * 2) : outerW,
            height: outerH > winH ? (winH - 88 * 2) : outerH,
            overflow: "scroll"
        });
        window.setTimeout(function () {
            ajax_load.remove();
        }, 30000);
        // alert(ajax_load.css("top"));
        // alert(ajax_load.css("left"));

    }
